package cn.gov.pbc.gd.credit.user.service.role.impl;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.liu.jbase.ResultVo;
import com.liu.jbase.annotation.LogAnnotation;
import com.liu.jbase.service.impl.BaseServiceImpl;
import com.liu.tools.LogUtil;

import cn.gov.pbc.gd.credit.api.corp.ICorpApi;
import cn.gov.pbc.gd.credit.api.menu.IMenuApi;
import cn.gov.pbc.gd.credit.api.role.IRoleApi;
import cn.gov.pbc.gd.credit.common.constant.CodeConstant;
import cn.gov.pbc.gd.credit.common.constant.LogConstant;
import cn.gov.pbc.gd.credit.common.model.UserModel;
import cn.gov.pbc.gd.credit.common.service.redis.IRedisService;
import cn.gov.pbc.gd.credit.common.util.UserUtil;
import cn.gov.pbc.gd.credit.user.service.role.IRoleService;
import cn.gov.pbc.gd.credit.vo.corp.ResCorpVo;
import cn.gov.pbc.gd.credit.vo.menu.ResMenuVo;
import cn.gov.pbc.gd.credit.vo.role.ResRoleVo;
import cn.gov.pbc.gd.credit.vo.role.RoleVo;

@Service
public class RoleServiceImpl extends BaseServiceImpl<ResRoleVo, RoleVo, IRoleApi> implements IRoleService{

	private Logger logger = LoggerFactory.getLogger(RoleServiceImpl.class);
	
	@Autowired
	private IRoleApi roleApi;
	
	@Autowired
	private IMenuApi menuApi;
	
	@Autowired
	private ICorpApi corpApi;
	
	@Autowired
	private IRedisService redisService;
	
	@Override
	public ResultVo<ResRoleVo> find(RoleVo record, HttpServletRequest request) {

		ResultVo<ResRoleVo> resultVo = new ResultVo<>();
		
		resultVo.setCode(CodeConstant.SUCCESS);
		
		ResRoleVo resRoleVo = roleApi.query(record);
		if(resRoleVo == null){
			resultVo.setCode(CodeConstant.DATA_NOT_EXSIT);
			logger.error(LogUtil.msg("RoleServiceImpl:find", "查询不到数据，reqData=" + JSON.toJSONString(record)));
			return resultVo;
		}
		
		if(record.getIsModify() != null && record.getIsModify() == 1){//修改
			
			List<ResMenuVo> list = menuApi.treeMenuList(null);
			
			List<ResMenuVo> userMenuList = resRoleVo.getUseMenuList();
			
			List<String> selectList = new ArrayList<>();
			
			if(userMenuList!=null&&userMenuList.size()>0){
				for (int i = 0; i <userMenuList.size(); i++) {
					selectList.add(userMenuList.get(i).getMenuId());
				}
				
				int subItemCount=0;
				for (int j = 0; j <list.size(); j++) {
					String rootMenuId=list.get(j).getId();
					List<ResMenuVo> subList=list.get(j).getChildList();
					
					for (int k = 0; k < subList.size(); k++) {
						String subMenuId=subList.get(k).getId();
						if(selectList.contains(subMenuId)){
							subList.get(k).setIsCheck(1);
							//统计子菜单选中几个，全选就把父菜单也选中
							subItemCount++;
						}else{
							subList.get(k).setIsCheck(0);
						}
					}
					
					//当用户选择列表包含根menuId设置为选中状态 或者当子全选全选时候根也设置为全选
					if(selectList.contains(rootMenuId)||subList.size()>0&&subItemCount==subList.size()){
						list.get(j).setIsCheck(1);
					}else{
						list.get(j).setIsCheck(0);
					}
					
					//清除上次统计的数目
					subItemCount=0;
					
				}
			}
			
			resRoleVo.setMenuList(list);
		}
		
		resultVo.setData(resRoleVo);
		return resultVo;
	}
	
	@LogAnnotation(moduleName=LogConstant.MODULE_ROLE, option=LogConstant.ADD, logType=LogConstant.LOG_OPERATE, content="添加角色")
	@Override
	public ResultVo<ResRoleVo> add(RoleVo record, HttpServletRequest request) {
		// TODO Auto-generated method stub
		return super.add(record, request);
	}
	@LogAnnotation(moduleName=LogConstant.MODULE_ROLE, option=LogConstant.UPDATE, logType=LogConstant.LOG_OPERATE, content="修改角色")
	@Override
	public ResultVo<ResRoleVo> update(RoleVo record, HttpServletRequest request) {
		// TODO Auto-generated method stub
		return super.update(record, request);
	}
	@LogAnnotation(moduleName=LogConstant.MODULE_ROLE, option=LogConstant.DEL, logType=LogConstant.LOG_OPERATE, content="删除角色")
	@Override
	public ResultVo<ResRoleVo> delete(RoleVo record, HttpServletRequest request) {
		// TODO Auto-generated method stub
		return super.delete(record, request);
	}
	
	@Override
	public ResultVo<List<ResRoleVo>> list(RoleVo record, HttpServletRequest request) {

		UserModel userModel = UserUtil.getUserModel(record.getSid(), redisService);
		ResCorpVo resCorpVo = corpApi.queryById(userModel.getCorpId());
		if(! "-1".equals(resCorpVo.getParentId())){
			record.setCorpId(userModel.getCorpId());
		}
		
		return super.list(record, request);
	}
}
